﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Sprites;

namespace Mars.MeshUI
{
    public class MeshBakerWidget : BaseMeshEffect
    {
        [SerializeField] public Mesh subMesh = default;

        public override void ModifyMesh(VertexHelper vh)
        {
            if (subMesh == null)
            {
                subMesh = new Mesh();
            }

            vh.FillMesh(subMesh);

            var graphic = GetComponent<Graphic>();

            MeshCanvas root = GetComponentInParent<MeshCanvas>();

            if (graphic == null)
            {
                return;
            }

            if (graphic is Img)
            {
                Img image = graphic as Img;

                MeshImg meshImg = GetComponent<MeshImg>();

                if (meshImg != null)
                {
                    Vector4 rect = GetDrawingDimensions(image.sprite, image.rectTransform.rect);

                    Vector3 pos = meshImg.transform.TransformPoint(new Vector3(rect.x, rect.y, 0));

                    Vector3 size = meshImg.transform.TransformPoint(new Vector3(rect.z, rect.w, 0));

                    pos = root.transform.InverseTransformPoint(pos);

                    size = root.transform.InverseTransformPoint(size);

                    meshImg.rect = new Vector4(pos.x, pos.y, size.x, size.y);

                    meshImg.size = image.rectTransform.rect.size;
                    meshImg.pivot = image.rectTransform.pivot;

                    meshImg.activeSprite = image.sprite;

                    if (image.type == Image.Type.Filled && image.fillMethod == Image.FillMethod.Radial360)
                    {
                        meshImg.AddRadial360(subMesh);
                        meshImg.type = Image.Type.Filled;
                    }
                    else
                    {
                        meshImg.SetVertex(subMesh, root.transform);

                        if (image.type == Image.Type.Sliced)
                        {
                            meshImg.type = Image.Type.Sliced;
                            meshImg.SetSliceLTRB();
                        }
                    }
                }
            }
        }

        private Vector4 GetDrawingDimensions(Sprite activeSprite, Rect r)
        {
            var padding = activeSprite == null ? Vector4.zero : DataUtility.GetPadding(activeSprite);
            var size = activeSprite == null
                ? Vector2.zero
                : new Vector2(activeSprite.rect.width, activeSprite.rect.height);

            // Debug.Log(string.Format("r:{2}, size:{0}, padding:{1}", size, padding, r));

            int spriteW = Mathf.RoundToInt(size.x);
            int spriteH = Mathf.RoundToInt(size.y);

            var v = new Vector4(
                padding.x / spriteW,
                padding.y / spriteH,
                (spriteW - padding.z) / spriteW,
                (spriteH - padding.w) / spriteH);

            v = new Vector4(
                r.x + r.width * v.x,
                r.y + r.height * v.y,
                r.x + r.width * v.z,
                r.y + r.height * v.w
            );

            return v;
        }
    }
}